package edu.xjtlu.mobilewebservices.map;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import edu.xjtlu.mobilewebservices.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class GPS2Map extends MapActivity{
	
	private MapView mapview;
	private MapController mc;
	private LocationManager locationManager;
	private MyOverlays itemizedoverlay;
	private MyLocationOverlay myLocationOverlay;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        //to display zoom control in MapView 
        mapview = (MapView) findViewById(R.id.mapview);
       
        mapview.setSatellite(true);
        //mapview.setTraffic(true);
        mapview.setBuiltInZoomControls(true);
        mapview.setClickable(true);
        mapview.displayZoomControls(true);
        mc = mapview.getController();
        mc.setZoom(18);
        
        
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
        
        myLocationOverlay = new MyLocationOverlay(this, mapview);
        mapview.getOverlays().add(myLocationOverlay);
        
        myLocationOverlay.runOnFirstFix(new Runnable(){

			@Override
			public void run() {
				mc.animateTo(myLocationOverlay.getMyLocation());
			}
        });
       
        
        Drawable drawable = this.getResources().getDrawable(R.drawable.locationtag);
        Log.v("-----ok--------LocationTag","");
        itemizedoverlay = new MyOverlays(this, drawable);
        mapview.getOverlays().add(itemizedoverlay);
	}


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	

	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			Log.v("Latitude", Integer.toString(lat));
			Log.v("Longitude", Integer.toString(lat));
			GeoPoint point = new GeoPoint(lat, lng);
			mc.animateTo(point);
			Log.v("-----ok--------GeoUpdateHandler","");
		}

		@Override
		public void onProviderDisabled(String arg0) {
			
		}

		@Override
		public void onProviderEnabled(String arg0) {
			
		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		myLocationOverlay.enableMyLocation();
		myLocationOverlay.enableCompass();
	}

	@Override
	protected void onPause() {
		super.onResume();
		myLocationOverlay.disableMyLocation();
		myLocationOverlay.disableCompass();
	}
}

	